package com.bracketbird.client.url;

import com.bracketbird.client.*;
import com.bracketbird.client.gui.club.tournament.*;
import com.bracketbird.client.gui.club.tournament.rtc.*;
import com.bracketbird.client.gui.pages.*;
import com.bracketbird.client.model.*;
import com.bracketbird.client.model.keys.*;
import com.bracketbird.client.service.*;
import com.google.gwt.user.client.*;
import com.bracketbird.clientcore.appcontrol.*;
import com.bracketbird.clientcore.model.keys.*;
import com.bracketbird.clientcore.service.*;
import com.bracketbird.clientcore.util.*;

import java.util.*;



/**
 *
 */
public class EditTournament implements Command {

    public static String name = "EDIT_TOURNAMENT";
    private static GlobalKeyboardHandler gh = new GlobalKeyboardHandler();

    public void execute(Map<String, String> pMap) {
        //Color.setScheme(new ColorSchemeRTC());

        Long invalidDate = Long.valueOf(pMap.get(UrlParam.INVALID_DATE));
        if (new Date().getTime() > invalidDate) {
            SinglePageNotice spn = new SinglePageNotice("Url is no longer valid", "Because of security reasons, this url is only refreshable one hour.");
            spn.getSubtitleLabel().setText("When running a tournament you should not refresh the site. Go to Bracketbird and enter the tournament again.");
            spn.show();
            return;
        }

        gh.setupSingleAppKeyboardShortcuts();

        PageFlow.activeController = RunningTournamentPageController.getInstance();

        ClubId clubId = new ClubId(pMap.get(UrlParam.CLUB_ID));
        TournamentId tournamentId = new TournamentId(pMap.get(UrlParam.TOURNAMENT_ID));
        final UserId userId = new UserId(pMap.get(UrlParam.USER_ID));

        StringBuffer sb = new StringBuffer();
        sb.append("UserId:").append(userId);
        sb.append("ClubId:").append(clubId);
        sb.append("TournamentId:").append(tournamentId);


        BBService.log(LogAction.Type.info, sb.toString(), null);

        BBService.getSingleAppInfo(userId, clubId, tournamentId, true, new CallBack<SingleResult<SingleAppInfo>>() {
            @Override
            public void success(SingleResult<SingleAppInfo> result) {
                UserManager.getInstance().setUserSilent(result.getResult().getUser());
                UserManager.getInstance().setClubSilent(result.getResult().getClub());
                UserManager.getInstance().setMembers(result.getResult().getMembers());

                RTC.getInstance().loadTournament(result.getResult().getTournament());

                for (Member m : result.getResult().getMembers()) {
                    if (m.getUserId().equals(userId)) {
                        UserManager.getInstance().setMemberSilent(m);
                    }
                }

                String tName = result.getResult().getTournament().getName();
                Window.setTitle(tName);
                RunningTournamentTop.getInstance().getTournamentName().setText(tName);

                PageFlow.show(TeamsPageController.getInstance());
                RTC.getInstance().loadServerSync(result.getResult().getTournamentEventLog());


            }

            @Override
            public void fail(Throwable t) {
            }
        });

    }


    public String getName() {
        return name;
    }

}